package study.itwill.common;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class Util {
	
	// Logger 클래스의 인스턴스를 받아온다.
	static Logger logger = Logger.getLogger(Util.class);
	
	/** 두 개의 정수 사이에서 난수를 리턴하는 메서드 */
	public static int random(int min, int max) {
		
		logger.debug("=============== method start >> random");
		
		int num = (int)((Math.random() * (max - min + 1)) + min);
		
		logger.debug("=============== method end >> random");
		
		return num;
	}
	
	/** HTML 태그를 제거하는 정규표현식 */
	public static String removeTag(String content) {
		
		logger.debug("=============== method start >> removeTag");
		
		Pattern SCRIPTS = Pattern.compile("<(no)?script[^>]*>.*?</(no)?script>",Pattern.DOTALL);
		Pattern STYLE = Pattern.compile("<style[^>]*>.*</style>",Pattern.DOTALL);
		Pattern TAGS = Pattern.compile("<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>");
		//Pattern nTAGS = Pattern.compile("<\\w+\\s+[^<]*\\s*>");
		Pattern ENTITY_REFS = Pattern.compile("&[^;]+;");
		Pattern WHITESPACE = Pattern.compile("\\s\\s+");
			
		Matcher m;
			
		m = SCRIPTS.matcher(content);
		content = m.replaceAll("");
		m = STYLE.matcher(content);
		content = m.replaceAll("");
		m = TAGS.matcher(content);
		content = m.replaceAll("");
		m = ENTITY_REFS.matcher(content);
		content = m.replaceAll("");
		m = WHITESPACE.matcher(content);
		content = m.replaceAll(" ");
		
		logger.debug("=============== method end >> removeTag");

		return content;
	}
	
	public static String getTime() {
		Calendar cal = Calendar.getInstance();
		int yy = cal.get(Calendar.YEAR);
		int mm = cal.get(Calendar.MONTH) + 1;
		int dd = cal.get(Calendar.DAY_OF_MONTH);
		int hh = cal.get(Calendar.HOUR_OF_DAY);
		int mi = cal.get(Calendar.MINUTE);
		int ss = cal.get(Calendar.SECOND);
		String time = String.format("%04d%02d%02d%02d%02d%02d",
				yy, mm, dd, hh, mi, ss);
		
		return time;
	}
}
